package com.portal.framework.common.lang;

/**
 * 单一个对象的缓冲
 */
public class ObjectCache<T> {

	private T cache;

	private transient long cacheTime = 0;

	private long maxCacheTime = 0;

	public ObjectCache() {
		super();
	}

	/**
	 * @param time
	 */
	public ObjectCache(long time) {
		super();
		maxCacheTime = time;
	}

	/**
	 * @param cache
	 * @param time
	 */
	public ObjectCache(long time, T cache) {
		super();
		this.cache = cache;
		maxCacheTime = time;
	}

	public T get() {
		synchronized (this) {
			if (cache != null && maxCacheTime > 0) {
				if (System.currentTimeMillis() - cacheTime > maxCacheTime) {
					cache = null;
				}
			}
			return cache;
		}
	}

	public void set(T cache) {
		synchronized (this) {
			cacheTime = System.currentTimeMillis();
			this.cache = cache;
		}
	}

	public long getMaxCacheTime() {
		return maxCacheTime;
	}

	public void setMaxCacheTime(long maxCacheTime) {
		this.maxCacheTime = maxCacheTime;
	}

	public long getCacheTime() {
		return cacheTime;
	}

	public void setCacheTime(long cacheTime) {
		this.cacheTime = cacheTime;
	}

}
